package com.kuangstudy.service.togetherfriend;

import com.kuangstudy.constant.RedisConstants;
import com.kuangstudy.entity.User;
import com.kuangstudy.service.focus.AttentionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author renyitong
 * @version 1.0.0
 * @createTime 2023年09月19日
 * @Description
 */
@Service
@Slf4j
public class TogetherFriendService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AttentionService attentionService;

    /**
     * 共同好友
     * @param userId1 飞哥
     * @param userId2 狂神
     */
    public List<User> together(Long userId1,Long userId2){
        // 1: 获取redis的set集合对象
        SetOperations<String, Long> opsForSet = redisTemplate.opsForSet();
        // 2 : 获取两个用户的集合
        // 获取飞哥的关注列表
        String key1 = RedisConstants.USER_FOCUS_LIST_SET + userId1;
        // 获取狂神的关注列表
        String key2 = RedisConstants.USER_FOCUS_LIST_SET + userId2;
        // 3: 对飞哥的关注列表和狂神的关注列表就行求交集
        Set<Long> integers = opsForSet.intersect(key1, key2);
        List<User> userList = attentionService.getUserInfo(integers);
        return userList;
    }

}
